//
//  WBStatusPictureView.swift
//  WBWeiBo
//
//  Created by 王斌 on 16/7/15.
//  Copyright © 2016年 王斌. All rights reserved.
//

import UIKit

//  重用标记
private let PictureViewCellIdentifier = "PictureViewCellIdentifier"

//  每张的间距
private let itemMargin:CGFloat = 5

//  每张的宽度
private let itemWith:CGFloat = (ScreeWidth - 2 * HomeCellMargin - 2 * itemMargin)/3


//   配图视图
class WBStatusPictureView: UICollectionView {

    //  配图数据源
    var picUrls:[WBStatusPictureInfo]?{
        didSet{
            messageLabel.text = "\(picUrls?.count ?? 0)"
            let size = calcPictureViewSize(picUrls?.count ?? 0)
            //  设置配图指定大小
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            
            //  重新加载数据
            self.reloadData()
            
        }
    }


    //  MARK : - 懒加载控件
    private lazy var messageLabel:UILabel = {
        
        let label = UILabel(textColor: UIColor.redColor(), fontSize: 30)
        return label
    }()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        //  设置大小
        flowLayout.itemSize = CGSize(width: itemWith, height: itemWith)
        
        //  水平间距
        flowLayout.minimumInteritemSpacing = itemMargin
        
        //  垂直间距
        flowLayout.minimumLineSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        //  注册cell
        registerClass(WBStatusPictureViewCell.self, forCellWithReuseIdentifier: PictureViewCellIdentifier)
        
        //  设置数据源代理
        dataSource = self
        
        addSubview(messageLabel)
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
    
        }
    }
    
    /*
       根据图片个数计算配图大小
    
       - parameter count: 配图个数
    
       - returns:配图大小
    */
    
    private func calcPictureViewSize(count:Int) ->CGSize{
        
        //  列数
        let cols = count > 3 ? 3 : count
        
        //  行数
        let rows = (count - 1) / 3 + 1
        
        //  计算当前配图的一个宽度
        let width = CGFloat(cols) * itemWith + CGFloat(cols - 1) * itemMargin
        
        //  计算当前配图的一个高度
        let height = CGFloat(rows) * itemWith + CGFloat(rows - 1) * itemMargin
        
        let size = CGSize(width: width, height: height)
        
        return size
    }
 
}



    //  MARK : -- UICollectionViewDataSource
extension WBStatusPictureView:UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureViewCellIdentifier, forIndexPath: indexPath) as! WBStatusPictureViewCell
        
        //  设置模型
        cell.picInfo = picUrls![indexPath.item]
        
        return cell
    }
}





    // MARK : -  自定义配图cell
    class WBStatusPictureViewCell: UICollectionViewCell {
        
        //  设置模型
        var picInfo:WBStatusPictureInfo?{
            didSet{
                if let imageUrl = picInfo?.thumbnail_pic{
                    imageView.sd_setImageWithURL(NSURL(string: imageUrl),placeholderImage: UIImage(named: "timeline_image_placeholder"))
                    
                    //    判断我们的图片地址是否以.gif结尾
                    //     if imageUrl.hasSuffix(".gif") {
                    //        gifImageView.hidden = false
                    //       } else {
                    //          gifImageView.hidden = true
                    //              }
                    
                    //   可以抽取成一句代码
                    gifImageView.hidden = !imageUrl.hasSuffix(".gif")
                }
            }
        }
    
    
    //  MARK : - 懒加载控件
    //  配图的图片
    private lazy var imageView:UIImageView = {
        let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        
        //  保证图片的比例显示,图片会超出
        view.contentMode = UIViewContentMode.ScaleAspectFill
        
        //  剪切超出多余的图片
        view.clipsToBounds = true
        
        return view
    }()
    
    
        
    // MARK : - gif图片
    private lazy var gifImageView:UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
        
     
    // MARK : -  添加控件设置约束
        private func setupUI(){
            contentView.addSubview(imageView)
            contentView.addSubview(gifImageView)
            
            imageView.snp_makeConstraints { (make) -> Void in
                make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
            }
            gifImageView.snp_makeConstraints { (make) -> Void in
                make.trailing.equalTo(imageView)
                make.bottom.equalTo(imageView)
            }
        }
        

}
